package com.scalar.chiptrack.adaptor.wipfed;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.FabSortLot;
import com.scalar.chiptrack.adaptor.InventoryLot;
import com.scalar.chiptrack.adaptor.TestLot;
import com.scalar.chiptrack.adaptor.AssemblyLot;
import com.scalar.chiptrack.adaptor.oset.OSETDAO;
import com.scalar.chiptrack.adaptor.tsmc.TSMCDAO;
import com.scalar.chiptrack.adaptor.chartered.CharteredDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.FabSortAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.AssemblyAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.TestAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.InventoryLotDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.adaptor.dao.NewAdaptorDAO;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.ScalarException;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.Locale;
import java.util.GregorianCalendar;
import java.util.Iterator;

import java.io.File;

import jxl.Workbook;
import jxl.Sheet;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Dec 12, 2006
 * Time: 3:13:40 PM
 */
public class WipFedParser extends JobHandler implements AdaptorConstants
{
    HashMap m_fabDataMap        = new HashMap();
    HashMap m_dieBankDataMap    = new HashMap();
    HashMap m_assemblyDataMap   = new HashMap();
    HashMap m_testDataMap       = new HashMap();
    HashMap m_trDataMap         = new HashMap();
    HashMap m_fgDataMap         = new HashMap();
    HashMap m_tsmcMap           = new HashMap();
    HashMap m_osetMap           = new HashMap();

    ArrayList charteredFabList  = new ArrayList();
    ArrayList tsmcFabList       = new ArrayList();
    ArrayList osetAsssemblyList = new ArrayList();

    private Logger m_parserLogger       = null;
    private String m_dataOutputDir      = null;

    DBTransaction dbTransaction         = null;

    public WipFedParser() throws Exception
    {
        try
        {
            m_dataOutputDir = ConfigManager.getWipFedSheetDirectory();
            AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );
            m_parserLogger = AdaptorUtils.getDataParserLogger( m_dataOutputDir, "WIPFED" );

            FabSortAdaptorDAO.setLogger(m_parserLogger);
            AssemblyAdaptorDAO.setLogger(m_parserLogger);
            TestAdaptorDAO.setLogger(m_parserLogger);
            InventoryLotDAO.setLogger(m_parserLogger);
            CharteredDAO.setLogger(m_parserLogger);
            TSMCDAO.setLogger(m_parserLogger);
        }
        catch (ScalarException e)
        {
            log( "ERROR!! ERROR!! ERROR!! Exception while loading WIPFED output directory details from database, Exception: " + e );
            throw e;
        }
    }

    public static void main(String args[]) throws Exception
    {
        new WipFedParser().execute();
    }


    public void execute() throws Exception
    {

        AdaptorUtils.printHeader("WIPFED", CONSTANT_PARSER );
        
        File doneDestDir = new File(m_dataOutputDir + "/done");

        File wipDataoutDir = new File(m_dataOutputDir);

        File[] fileList = wipDataoutDir.listFiles();

        if ((fileList == null) || (fileList.length <= 3))
        {
            log("No file found in directory: " + wipDataoutDir);
            throw new ScalarException("No file found in directory: " + wipDataoutDir + "----\n\n");
        }

        ArrayList successFileList   = new ArrayList();
        boolean success             = false;

        //PARSING STARTS FROM HERE
        for (int i = 0; i < fileList.length; i++)
        {
            if (fileList[i].isDirectory() == true)
            {
                continue;
            }

            if ( (fileList[i].getName() != null) && fileList[i].getName().trim().startsWith(WipFedAdaptor.FILE_NAME_WIP_STATUS ) )
            {
                if (  fileList[i].getName().trim().endsWith(".xls") )
                {
                    ArrayList m_wipStatusList = parsewipfedInfosheet( fileList[i].getAbsolutePath() );

                    if ( m_wipStatusList != null && m_wipStatusList.size() > 0)
                    {
                        getWipFedDetails( m_wipStatusList );
                    }
                }
                successFileList.add( fileList[i] );
            }
        }

         /*Updating the Chartered Fab Lot Information*/
        try
        {
            // Step 1
            // Save Fab Information
            if ( charteredFabList != null && charteredFabList.size() > 0 )
            {
               log( " Updating Wip Fed Chartered Fab Lot Information" );
               CharteredDAO.saveWipFedCharteredDetails( dbTransaction, charteredFabList );

               charteredFabList.clear();
               charteredFabList = null;
            }
            success = true;
        }
        catch ( ScalarException se )
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Fab Lot information into database: " + se );
            throw se;
        }

        /*Updating the TSMCFab Lot Information*/
        try
        {
            // Step 1
            // Save Fab Information
            if ( tsmcFabList != null && tsmcFabList.size() > 0 )
            {
               log( " Updating Wip Fed TSMC Fab Lot Information" );
              TSMCDAO.saveWipFedTSMCDetails( dbTransaction, tsmcFabList);
              tsmcFabList.clear();
              tsmcFabList = null;
            }
            success = true;
        }
        catch ( ScalarException se )
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Fab Lot information into database: " + se );
            throw se;
        }

        /*Updating the Fab Lot Information*/
        try
        {
            // Step 1
            // Save Fab Information
            if ( m_fabDataMap.isEmpty() == false )
            {
               log( " Updating Fab Lot Information" );
               FabSortAdaptorDAO.saveFabLot( dbTransaction, m_fabDataMap.values().iterator(), LOCATION_TYPE_FAB , null ) ;
            }
            else
            {
                FabSortAdaptorDAO.closeFabSortLot( dbTransaction, LOCATION_TYPE_FAB, null );
            }
            m_fabDataMap.clear();
            m_fabDataMap = null;
            success = true;
        }
        catch ( ScalarException se )
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Fab Lot information into database: " + se );
            throw se;
        }

        /* Updating lots from Wip fed and Chartered */
        try
        {
            HashMap m_charteredMap =CharteredDAO.getCharteredFabList();
            if ( m_charteredMap.isEmpty() == false )
            {
                log( " Updating Wip Fed Chartered Fab Lot Information in to  Fab lot Table" );
                FabSortAdaptorDAO.saveFabLot( null,  m_charteredMap.values().iterator(), LOCATION_TYPE_FAB, VENDOR_NAME_CHARTERED );
            }
            m_charteredMap.clear();
            m_charteredMap = null;

            success = true;
        }
        catch( Exception e)
        {
            success = false;
            e.printStackTrace();
            throw e;
        }

        /* Updating lots from Wip fed and Chartered */
       try
        {
            m_tsmcMap = TSMCDAO.getTSMCFabList();

            log( " Updating Wip Fed TSMC Fab Lot Information in to  Fab lot Table" );
            FabSortAdaptorDAO.saveFabLot( null, m_tsmcMap.values().iterator(), LOCATION_TYPE_FAB, VENDOR_NAME_TSMC );
            m_tsmcMap.clear();
            m_tsmcMap = null;
        }
        catch( Exception e)
        {
            success = false;
            e.printStackTrace();
        }

        /*Updating the Die Bank Lot Information*/
        try
        {
            // Save Die Bank Information
            if( m_dieBankDataMap.isEmpty() == false )
            {
                log(" Updating Die Bank Lot Information");
                InventoryLotDAO.saveInventoryLot( dbTransaction, m_dieBankDataMap.values().iterator(), INVENTORY_TYPE_DIE_BANK, null );
            }
            else
            {
                InventoryLotDAO.closeInventoryLot( dbTransaction, INVENTORY_TYPE_DIE_BANK, null);
            }
            m_dieBankDataMap.clear();
            m_dieBankDataMap = null;
            success = true;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Die Bank Lot information into database: " + se);
            throw se;
        }

        /*Updating the Assembly Lot Information*/
        try
        {
            // Step 3
            // Save Assembly Information
            if( osetAsssemblyList.isEmpty() == false )
            {
                log(" Updating OSET Assembly Lot Information in to Intermediate table");
                OSETDAO.saveWipFedOSETDetails( null, osetAsssemblyList, false );
            }
            else
            {
                OSETDAO.saveWipFedOSETDetails( null, osetAsssemblyList, true );
            }
            osetAsssemblyList.clear();
            osetAsssemblyList = null;
            success = true;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Assembly Lot information into Intermediate tables: " + se);
            throw se;
        }



        /*Updating the Assembly Lot Information*/
        try
        {
            // Step 3
            // Save Assembly Information
            if( m_assemblyDataMap.isEmpty() == false )
            {
                log(" Updating Assembly Lot Information");
                AssemblyAdaptorDAO.saveAssemblyLot( dbTransaction, m_assemblyDataMap.values().iterator(), WIP_TYPE_ASSEMBLY, null );
            }
            else
            {
                AssemblyAdaptorDAO.closeAssemblyLot( dbTransaction, WIP_TYPE_ASSEMBLY, null );
            }
            m_assemblyDataMap.clear();
            m_assemblyDataMap = null;
            success = true;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Assembly Lot information into database: " + se);
            throw se;
        }


        /* Updating lots from Wip fed and Chartered */
       try
        {
            m_osetMap = OSETDAO.getOSETAssemblyMap();
            log( " Updating OSET Assembly information from Wip Fed and OSET " );
            AssemblyAdaptorDAO.saveAssemblyLot( null, m_osetMap.values().iterator(), WIP_TYPE_ASSEMBLY, VENDOR_NAME_OSET );
            m_osetMap.clear();
            m_osetMap = null;
        }
        catch( Exception e)
        {
            success = false;
            e.printStackTrace();
        }

         /*Updating the Test Lot Information*/
        try
        {
            // Step 3
            // Save Test Information
            if( m_testDataMap.isEmpty() == false )
            {

                log(" Updating Test Lot Information");
                TestAdaptorDAO.saveTestLot( dbTransaction, m_testDataMap.values().iterator(), LOCATION_TYPE_FT, null );
            }
            else
            {
                TestAdaptorDAO.closeTestLot( dbTransaction, LOCATION_TYPE_FT, null);
            }
            m_testDataMap.clear();
            m_testDataMap = null;
            success = true;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Test Lot information into database: " + se);
            throw se;
        }

        /*Updating the Class Store Lot Information*/
        try
        {
            // Step 2
            // Save Class Store Information
            if( m_trDataMap.isEmpty() == false )
            {
                log(" Updating Class Store Lot Information");
                InventoryLotDAO.saveInventoryLot( dbTransaction, m_trDataMap.values().iterator(), LOCATION_TYPE_TAPE_REEL, null );
            }
            else
            {
                InventoryLotDAO.closeInventoryLot( dbTransaction, LOCATION_TYPE_TAPE_REEL, null);
            }
            m_trDataMap.clear();
            m_trDataMap = null;
            success = true;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Class Store Lot information into database: " + se);
            throw se;
        }

        /*Updating the FG Lot Information*/
        try
        {
            // Step 2
            // Save Finished Goods Information
            if( m_fgDataMap.isEmpty() == false )
            {
                log(" Updating Finished Goods Lot Information");
                InventoryLotDAO.saveInventoryLot( dbTransaction, m_fgDataMap.values().iterator(), LOCATION_TYPE_FG, null );
            }
            else
            {
                InventoryLotDAO.closeInventoryLot( dbTransaction, LOCATION_TYPE_FG, null);
            }
            m_fgDataMap.clear();
            m_fgDataMap = null;
            success = true;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Finished Goods Lot information into database: " + se);
            throw se;
        }

        // TO SEND THE NEW LOTS, NEW TRANSIT LOTS, NO PO LOTS, DISCREPANCY LOTS NOTIFICATIONS TO THE USER
        AdaptorDAO.sendAllNotifications( VENDOR_NAME_CHARTERED, true, true, false);
        AdaptorDAO.sendAllNotifications( VENDOR_NAME_TSMC , true, true, false);
        AdaptorDAO.sendAllNotifications( VENDOR_NAME_ATP,   true, true, false );
        AdaptorDAO.sendAllNotifications( VENDOR_NAME_CMI ,  true, true, false);
        AdaptorDAO.sendAllNotifications( VENDOR_NAME_CML ,  true, true, false);
        AdaptorDAO.sendAllNotifications( VENDOR_NAME_CMT,   true, true, false );
        AdaptorDAO.sendAllNotifications( VENDOR_NAME_ATK ,  true, true, false);
        AdaptorDAO.sendAllNotifications( VENDOR_NAME_OSET,  true, true, false );

         // To Close the Drop Out Lots
        AdaptorDAO.closeDropOutLots( VENDOR_NAME_CMI );
        AdaptorDAO.closeDropOutLots( VENDOR_NAME_TSMC );
        AdaptorDAO.closeDropOutLots( VENDOR_NAME_CHARTERED );
        AdaptorDAO.closeDropOutLots( VENDOR_NAME_CML );
        AdaptorDAO.closeDropOutLots( VENDOR_NAME_CMT );
        AdaptorDAO.closeDropOutLots( VENDOR_NAME_ATK );
        AdaptorDAO.closeDropOutLots( VENDOR_NAME_OSET );
        AdaptorDAO.closeDropOutLots( VENDOR_NAME_ATP );

        
        int adaptor_id = AdaptorRunInfo.getLastRunAdaptorId( WipFedAdaptor.ADAPTOR_NAME, CONSTANT_ADAPTOR);

        // Update Adaptor_Run_Info table
        try
        {
            if ( success == true )
            {
                AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, WipFedAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
                System.out.println( "SUCCESSFULLY UPDATED IN THE DATABASE" );
            }
        }
        catch ( ScalarException se )
        {
            success = false; //As filES DATA SAVED , UPDATEING AS SUCCESS
            AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, WipFedAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
            log( "WARNING!! WARNING!! WARNING!! Could not update Adaptor run information, Exception: " + se );
            throw se;
        }
        // Move the successful files to 'done' directory
       if ( success )
       {
           AdaptorUtils.moveFilesToDone( successFileList, doneDestDir );
       }
    }//end of execute method

    /**
     *
     * @param fileName
     * @return
     * @throws Exception
     */
    private ArrayList parsewipfedInfosheet(String fileName) throws Exception
    {
        if ( fileName == null )
        {
            throw new ScalarException( "Filename argument is null" );
        }

        WipFedStatus wipFedStatus       = null;
        ArrayList wipfedList                  = new ArrayList();

        try
        {
            File wipFedFile = new File( fileName );
            if ( wipFedFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "Parsing the file for Wip Fed Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "Parsing the file for  Wip Fed Report..." );

            Workbook workbook = Workbook.getWorkbook( wipFedFile );
            Sheet sheet = workbook.getSheet( 0 );

            Cell[] row = null;

            for ( int rowCount = 3; rowCount < sheet.getRows(); rowCount++ )
            {
                row = sheet.getRow( rowCount );

                wipFedStatus = new WipFedStatus();

                if( row.length > 0 )
                {
                    if ( row[0].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setLotNumber( null );
                    }
                    else
                    {
                        wipFedStatus.setLotNumber( row[0].getContents().trim() );
                    }

                }
                if( row.length > 1)
                {
                    if ( row[1].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setSplit( null );
                    }
                    else
                    {
                        wipFedStatus.setSplit( row[1].getContents().trim() ) ;
                    }
                }
                if( row.length > 2)
                {
                    if ( row[2].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setMfgArea(null );
                    }
                    else
                    {
                        wipFedStatus.setMfgArea( row[2].getContents().trim() );
                    }
                }
                if( row.length > 3)
                {
                    if ( row[3].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setFeStartPart( null );
                    }
                    else
                    {
                        wipFedStatus.setFeStartPart( row[3].getContents().trim() );
                    }
                }
                if ( row.length > 4 )
                {
                   if ( row[4].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setFeStartRev( null );
                    }
                    else
                    {
                        wipFedStatus.setFeStartRev( row[4].getContents().trim() );
                    }
                }
                if( row.length > 5 )
                {
                    if ( row[5].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setDiePart( null );
                    }
                    else
                    {
                        wipFedStatus.setDiePart( row[5].getContents().trim() );
                    }

                }
                if( row.length > 6 )
                {
                    if ( row[6].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setDieRev( null );
                    }
                    else
                    {
                        wipFedStatus.setDieRev( row[6].getContents().trim() );
                    }
                }
                if ( row.length > 7 )
                {
                    if ( row[7].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setBePart( null );
                    }
                    else
                    {
                        wipFedStatus.setBePart( row[7].getContents().trim() );
                    }
                }
                if( row.length > 8 )
                {
                    if ( row[8].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setBeRev( null );
                    }
                    else
                    {
                        wipFedStatus.setBeRev( row[8].getContents().trim() );
                    }
                }
                if( row.length > 9 )
                {
                    if ( row[9].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setPkg( null );
                    }
                    else
                    {
                        wipFedStatus.setPkg( row[9].getContents().trim() );
                    }
                }
                if( row.length >10 )
                {
                    if ( row[10].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setTempRange( null );
                    }
                    else
                    {
                        wipFedStatus.setTempRange( row[10].getContents().trim() ) ;
                    }
                }
                if( row.length >11 )
                {
                    if ( row[11].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setBin( null );
                    }
                    else
                    {
                        wipFedStatus.setBin( row[11].getContents().trim() );
                    }
                }
                if( row.length > 12 )
                {
                    if ( row[12].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setPkgCode( null );
                    }
                    else
                    {
                        wipFedStatus.setPkgCode( row[12].getContents().trim() );
                    }
                }
                if ( row.length > 13 )
                {
                    if ( row[13].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setFisrtBin( null );
                    }
                    else
                    {
                        wipFedStatus.setFisrtBin( row[13].getContents().trim() );
                    }
                }
                if( row.length > 14 )
                {
                    if ( row[14].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setMesStep( null );
                    }
                    else
                    {
                        wipFedStatus.setMesStep( row[14].getContents().trim() );
                    }
                }
                if( row.length > 15 )
                {
                    if ( row[15].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setMfgLoc( null );
                    }
                    else
                    {
                        wipFedStatus.setMfgLoc( row[15].getContents().trim() ) ;
                    }
                }
                if( row.length > 16 )
                {
                    if ( row[16].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setPreMfgLoc( null );
                    }
                    else
                    {
                        wipFedStatus.setPreMfgLoc( row[16].getContents().trim() ) ;
                    }
                }
                if( row.length > 17 )
                {
                    if ( row[17].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setNextMfgLoc( null );
                    }
                    else
                    {
                        wipFedStatus.setNextMfgLoc( row[17].getContents().trim() ) ;
                    }
                }
                if( row.length > 18 )
                {

                     if (row[18].getType().equals(CellType.EMPTY))
                    {
                        wipFedStatus.setCsDate(null);
                    }
                    else if( row[18].getType().equals( CellType.DATE ) )
                    {
                        DateCell dc = (DateCell) row[18];
                        java.util.Date csDate = null;
                        csDate = dc.getDate();

                        if ("America/Los_Angeles".equals(System.getProperty("user.timezone")))
                        {
                            Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.US );
                            cal.setTime(csDate);
                            csDate = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipFedStatus.setCsDate( StringUtils.formatDateToString( csDate )  ) ;
                        }
                        else
                        {
                            Calendar cal = new GregorianCalendar();
                            cal.setTime(csDate);
                            csDate = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipFedStatus.setCsDate(StringUtils.formatDateToString(csDate));
                        }
                    }
                }
                if( row.length > 19 )
                {

                    if (row[19].getType().equals(CellType.EMPTY))
                    {
                        wipFedStatus.setRsDate(null);
                    }
                    else if( row[19].getType().equals( CellType.DATE ) )
                    {
                        DateCell dc = (DateCell) row[19];
                        java.util.Date rsDate = null;
                        rsDate = dc.getDate();

                        if ("America/Los_Angeles".equals(System.getProperty("user.timezone")))
                        {
                            Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.US );
                            cal.setTime(rsDate);
                            rsDate = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipFedStatus.setRsDate( StringUtils.formatDateToString( rsDate )  ) ;
                        }
                        else
                        {
                            Calendar cal = new GregorianCalendar();
                            cal.setTime(rsDate);
                            rsDate = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipFedStatus.setRsDate( StringUtils.formatDateToString( rsDate )  ) ;
                        }
                    }
                }
                if( row.length > 20 )
                {

                    if (row[20].getType().equals(CellType.EMPTY))
                    {
                        wipFedStatus.setOsDate(null);
                    }
                    else if( row[20].getType().equals( CellType.DATE ) )
                    {
                        DateCell dc = (DateCell) row[20];
                        java.util.Date osDate = null;
                        osDate = dc.getDate();

                        if ("America/Los_Angeles".equals(System.getProperty("user.timezone")))
                        {
                            Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.US );
                            cal.setTime(osDate);
                            osDate = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipFedStatus.setOsDate( StringUtils.formatDateToString( osDate )  ) ;
                        }
                        else
                        {
                            Calendar cal = new GregorianCalendar();
                            cal.setTime(osDate);
                            osDate = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipFedStatus.setOsDate( StringUtils.formatDateToString( osDate )  ) ;
                        }
                    }
                }
                if( row.length > 21 )
                {
                    if (row[21].getType().equals(CellType.EMPTY))
                    {
                        wipFedStatus.setStartDate(null);
                    }
                    else if( row[21].getType().equals( CellType.DATE ) )
                    {
                        DateCell dc = (DateCell) row[21];
                        java.util.Date dateIn = null;
                        dateIn = dc.getDate();

                        if ("America/Los_Angeles".equals(System.getProperty("user.timezone")))
                        {
                            Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.US );
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipFedStatus.setStartDate( StringUtils.formatDateToString( dateIn )  ) ;
                        }
                        else
                        {
                            Calendar cal = new GregorianCalendar();
                            cal.setTime(dateIn);
                            dateIn = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipFedStatus.setStartDate(StringUtils.formatDateToString(dateIn));
                        }
                    }
                }
                if( row.length > 22 )
                {

                    if (row[22].getType().equals(CellType.EMPTY))
                    {
                        wipFedStatus.setOutDate(null);
                    }
                    else if( row[22].getType().equals( CellType.DATE ) )
                    {
                        DateCell dc = (DateCell) row[22];
                        java.util.Date outDate = null;
                        outDate = dc.getDate();
                        if ("America/Los_Angeles".equals(System.getProperty("user.timezone")))
                        {
                            Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ), Locale.US );
                            cal.setTime(outDate);
                            outDate = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipFedStatus.setOutDate( StringUtils.formatDateToString( outDate )  ) ;
                        }
                        else
                        {
                            Calendar cal = new GregorianCalendar();
                            cal.setTime(outDate);
                            outDate = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)).getTime();
                            wipFedStatus.setOutDate(StringUtils.formatDateToString(outDate));
                        }
                    }
                }
                if( row.length > 23 )
                {
                    if ( row[23].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setMktPart( null );
                    }
                    else
                    {
                        wipFedStatus.setMktPart( row[23].getContents().trim() );
                    }
                }
                if( row.length > 24 )
                {
                    if ( row[24].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setMfgPart( null );
                    }
                    else
                    {
                        wipFedStatus.setMfgPart( row[24].getContents().trim() );
                    }
                }
                if( row.length > 25 )
                {
                    if ( row[25].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setDevice( null );
                    }
                    else
                    {
                        wipFedStatus.setDevice( row[25].getContents().trim() ) ;
                    }
                }
                if( row.length > 26 )
                {
                    if ( row[26].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setMesStatus( null );
                    }
                    else
                    {
                        wipFedStatus.setMesStatus( row[26].getContents().trim() );
                    }
                }
                if( row.length > 27 )
                {
                    if ( row[27].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setDivision( null );
                    }
                    else
                    {
                        wipFedStatus.setDivision( row[27].getContents().trim() );
                    }
                }
                if( row.length > 28 )
                {
                    if ( row[28].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setMpStage( null );
                    }
                    else
                    {
                        wipFedStatus.setMpStage( row[28].getContents().trim() );
                    }
                }
                if( row.length > 29 )
                {
                    if ( row[29].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setMpLevel( null );
                    }
                    else
                    {
                        wipFedStatus.setMpLevel( row[29].getContents().trim() ) ;
                    }
                }
                if( row.length > 30 )
                {
                    if ( row[30].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setMpStatus( null );
                    }
                    else
                    {
                        wipFedStatus.setMpStatus( row[30].getContents().trim() ) ;
                    }
                }
                if( row.length > 31 )
                {
                    if ( row[31].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setMpTreatment( null );
                    }
                    else
                    {
                        wipFedStatus.setMpTreatment( row[31].getContents().trim() ) ;
                    }
                }
                if( row.length > 32 )
                {
                    if ( row[32].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setMesQty( null );
                    }
                    else
                    {
                        wipFedStatus.setMesQty( row[32].getContents().trim() ) ;
                    }
                }
                if( row.length > 33 )
                {
                    if ( row[33].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setMpQty( null );
                    }
                    else
                    {
                        wipFedStatus.setMpQty( row[33].getContents().trim() ) ;
                    }
                }
                if( row.length > 34 )
                {
                    if ( row[34].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setProdItem( null );
                    }
                    else
                    {
                        wipFedStatus.setProdItem( row[34].getContents().trim() ) ;
                    }
                }
                if( row.length > 35 )
                {
                    if ( row[35].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setProdLoc( null );
                    }
                    else
                    {
                        wipFedStatus.setProdLoc( row[35].getContents().trim() ) ;
                    }
                }
                if( row.length > 36 )
                {
                    if ( row[36].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setOpLoc( null );
                    }
                    else
                    {
                        wipFedStatus.setOpLoc( row[36].getContents().trim() ) ;
                    }
                }
                if( row.length > 37 )
                {
                    if ( row[37].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setTransactionSource( null );
                    }
                    else
                    {
                        wipFedStatus.setTransactionSource( row[37].getContents().trim() ) ;
                    }
                }
                if( row.length > 38 )
                {
                    if ( row[38].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setMrb( null );
                    }
                    else
                    {
                        wipFedStatus.setMrb( row[38].getContents().trim() ) ;
                    }
                }
                if( row.length > 39 )
                {
                    if ( row[39].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setInclude( null );
                    }
                    else
                    {
                        wipFedStatus.setInclude( row[39].getContents().trim() ) ;
                    }
                }
                if( row.length > 40 )
                {
                    if ( row[40].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setErrCode( null );
                    }
                    else
                    {
                        wipFedStatus.setErrCode( row[40].getContents().trim() ) ;
                    }
                }
                if( row.length > 41 )
                {
                    if ( row[41].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setWarnCode( null );
                    }
                    else
                    {
                        wipFedStatus.setWarnCode( row[41].getContents().trim() ) ;
                    }
                }
                if( row.length > 42 )
                {
                    if ( row[42].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setFePartNo( null );
                    }
                    else
                    {
                        wipFedStatus.setFePartNo( row[42].getContents().trim() ) ;
                    }
                }
                if( row.length > 43 )
                {
                    if ( row[43].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setDiePartNo( null );
                    }
                    else
                    {
                        wipFedStatus.setDiePartNo( row[43].getContents().trim() ) ;
                    }
                }
                if( row.length > 44 )
                {
                    if ( row[44].getType().equals(CellType.EMPTY) )
                    {
                        wipFedStatus.setBePartNo( null );
                    }
                    else
                    {
                        wipFedStatus.setBePartNo( row[44].getContents().trim() ) ;
                    }
                }
                if ( wipFedStatus.getLotNumber() != null  )
                {
                    wipfedList.add( wipFedStatus );
                }
            }
        }
        catch ( Exception ex )
        {
            ex.printStackTrace();
            throw ex;
        }

        return wipfedList;
    }

    public void getWipFedDetails( ArrayList wipFedList ) throws Exception
    {

        String partNumber       = null;
        String mktPartNumber    = null;
        String lotNumber        = null;
        WipFedStatus wipStatus  = null;

        FabSortLot fabLot           = null;
        InventoryLot inventoryLot   = null;
        AssemblyLot assemblyLot     = null;
        TestLot     testLot         = null;
        String psType               = null;

        try
        {

            Iterator wipFedIterator = wipFedList.iterator();

            while ( wipFedIterator.hasNext() )
            {
                wipStatus = (WipFedStatus) wipFedIterator.next();

                partNumber = wipStatus.getFabPartNumber();
                mktPartNumber = wipStatus.getMktPart();
                lotNumber = wipStatus.getLotNo();

                /* Fab Lots*/
                if( wipStatus.getMfgArea() != null && wipStatus.getMfgArea().startsWith("FAB") )
                {
                    if( partNumber == null )
                    {
                        partNumber = wipStatus.getBasePartNo();
                        psType     = LOCATION_TYPE_FAB;
                    }
                    else
                    {
                        psType     = LOCATION_TYPE_FAB_OPTION;
                    }

                    if( partNumber != null && lotNumber != null )
                    {
                        String vendorName = null;
                        if ( wipStatus.getMfgArea() != null && "FAB2".equalsIgnoreCase(wipStatus.getMfgArea()))
                        {
                            vendorName = VENDOR_NAME_CMT;
                        }
                        else if ( wipStatus.getMfgArea() != null && "FAB4".equalsIgnoreCase(wipStatus.getMfgArea()))
                        {
                            vendorName = VENDOR_NAME_CMI;
                        }
                        else if ( wipStatus.getMfgLoc() != null && "F".equalsIgnoreCase(wipStatus.getMfgLoc()))
                        {
                            vendorName = VENDOR_NAME_TSMC;
                        }
                        else if ( wipStatus.getMfgLoc() != null && "R".equalsIgnoreCase(wipStatus.getMfgLoc()))
                        {
                            vendorName = VENDOR_NAME_CHARTERED;
                        }

                        fabLot = ( FabSortLot) m_fabDataMap.get( lotNumber + partNumber + vendorName );

                        if( fabLot == null )
                        {
                            fabLot = new FabSortLot();
                            m_fabDataMap.put( lotNumber + partNumber + vendorName, fabLot );
                        }

                        int quantity = 0;
                        if( fabLot.getQtyIn() != null )
                        {
                            try
                            {
                                quantity = Integer.parseInt(fabLot.getQtyIn());
                            }
                            catch (NumberFormatException npe)
                            {
                            }
                            try
                            {
                                quantity += Integer.parseInt( wipStatus.getMesQty() );
                            }
                            catch (NumberFormatException npe)
                            {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt( wipStatus.getMesQty());
                            }
                            catch( NumberFormatException npe )
                            {
                            }
                        }
                        if( vendorName != null && quantity > 0 && quantity <= 25)
                        {
                            wipStatus.setPsType( psType );
                            if ( VENDOR_NAME_CHARTERED.equalsIgnoreCase( vendorName ) )
                            {
                                wipStatus.setCharteredPartNo( partNumber );
                                charteredFabList.add( wipStatus );
                            }
                            else if ( VENDOR_NAME_TSMC.equalsIgnoreCase( vendorName ))
                            {
                                wipStatus.setCharteredPartNo(partNumber);
                                tsmcFabList.add ( wipStatus );
                            }
                            else
                            {
                                fabLot.setLotId( lotNumber );
                                fabLot.setCustProduct( partNumber );
                                fabLot.setDateIn( wipStatus.getStartDate());
                                fabLot.setOutLookDate( wipStatus.getRsDate() );
                                fabLot.setForcastedDateOut( wipStatus.getOsDate() );
                                fabLot.setDateOut( wipStatus.getOutDate() );
                                fabLot.setQtyIn( quantity +"");
                                fabLot.setStage( wipStatus.getMesStep() );
                                fabLot.setLotStatus(LOT_STATUS_RUNNING);
                                fabLot.setVendorName( vendorName );
                                fabLot.setPsType( psType );
                                fabLot.setFabLotNumber(lotNumber);
                            }
                        }
                    }
                }
                /**
                 * If Mfg area is "Die" and MESStep Other than "DBank" we considering as Fab lots.
                 * Here some of of lots having more tahn 25 wafer Qty, we are ignoring that lots.
                 */
                if ( wipStatus.getMfgArea() != null && "DIE".equalsIgnoreCase( wipStatus.getMfgArea()) && wipStatus.getMesStep() != null && !"DBANK".equalsIgnoreCase( wipStatus.getMesStep()) && !"BKGRN".equalsIgnoreCase( wipStatus.getMesStep()))
                {
                    String vendorName = null;
                    if (wipStatus.getMfgLoc() != null && "R".equalsIgnoreCase(wipStatus.getMfgLoc()))
                    {
                        vendorName = VENDOR_NAME_CMI;
                    }
                    else if (wipStatus.getMfgLoc() != null && "T".equalsIgnoreCase(wipStatus.getMfgLoc()))
                    {
                        vendorName = VENDOR_NAME_TSMC;
                    }
                    else if (wipStatus.getMfgLoc() != null && "M".equalsIgnoreCase(wipStatus.getMfgLoc()))
                    {
                        vendorName = VENDOR_NAME_CMT;
                    }
                    fabLot = (FabSortLot) m_fabDataMap.get(lotNumber + partNumber + vendorName);

                    if (fabLot == null)
                    {
                        fabLot = new FabSortLot();
                        m_fabDataMap.put(lotNumber + partNumber + vendorName, fabLot);
                    }

                    int quantity = 0;
                    if (fabLot.getQtyIn() != null)
                    {
                        try
                        {
                            quantity = Integer.parseInt(fabLot.getQtyIn());
                        }
                        catch (NumberFormatException npe) {
                        }
                        try
                        {
                            quantity += Integer.parseInt(wipStatus.getMesQty());
                        }
                        catch (NumberFormatException npe) {
                        }
                    }
                    else
                    {
                        try
                        {
                            quantity = Integer.parseInt(wipStatus.getMesQty());
                        }
                        catch (NumberFormatException npe) {
                        }
                    }
                    if ( partNumber != null && lotNumber != null && vendorName != null && quantity <= 25)
                    {
                        fabLot.setLotId(lotNumber);
                        fabLot.setCustProduct(partNumber);
                        fabLot.setDateIn(wipStatus.getStartDate());
                        fabLot.setOutLookDate(wipStatus.getRsDate());
                        fabLot.setForcastedDateOut(wipStatus.getOsDate());
                        fabLot.setDateOut(wipStatus.getOutDate());
                        fabLot.setQtyIn(quantity + "");
                        fabLot.setStage(wipStatus.getMesStep());
                        fabLot.setLotStatus(LOT_STATUS_RUNNING);
                        fabLot.setVendorName(vendorName);
                        fabLot.setFabLotNumber(lotNumber);
                        fabLot.setPsType(LOCATION_TYPE_FAB_OPTION);
                    }
                }
                /* Die Bank Lots*/
                if ( wipStatus.getMfgArea() != null && "DIE".equalsIgnoreCase( wipStatus.getMfgArea()) && wipStatus.getMesStep() != null && ( "DBANK".equalsIgnoreCase( wipStatus.getMesStep()) || ("BKGRN".equalsIgnoreCase( wipStatus.getMesStep())) ))
                {
                    String vendorName = null;
                    if( wipStatus.getMfgLoc() != null && "R".equalsIgnoreCase( wipStatus.getMfgLoc()))
                    {
                        vendorName = VENDOR_NAME_CML;
                    }
                    else if( wipStatus.getMfgLoc() != null && "T".equalsIgnoreCase( wipStatus.getMfgLoc()))
                    {
                        vendorName = VENDOR_NAME_OSET;
                    }
                    else if( wipStatus.getMfgLoc() != null && "MM".equalsIgnoreCase( wipStatus.getMfgLoc()))
                    {
                        vendorName = VENDOR_NAME_CMI;
                    }
                    if ( partNumber != null && lotNumber != null && vendorName != null )
                    {
                        inventoryLot = ( InventoryLot) m_dieBankDataMap.get( lotNumber + partNumber + vendorName );
                        if( inventoryLot == null )
                        {
                            inventoryLot = new InventoryLot();
                            m_dieBankDataMap.put( lotNumber + partNumber + vendorName, inventoryLot );
                        }

                        int quantity = 0;
                        if( inventoryLot.getQtyIn() != null )
                        {
                            try
                            {
                                quantity = Integer.parseInt(inventoryLot.getQtyIn());
                            }
                            catch (NumberFormatException npe)
                            {
                            }
                            try
                            {
                                quantity += Integer.parseInt( wipStatus.getMesQty() );
                            }
                            catch (NumberFormatException npe)
                            {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt( wipStatus.getMesQty());
                            }
                            catch( NumberFormatException npe )
                            {
                            }
                        }
						if ( quantity > 0 )
						{						
							inventoryLot.setLotNumber( lotNumber );
							inventoryLot.setPartNumber( partNumber );
							inventoryLot.setDateIn( wipStatus.getStartDate());
							inventoryLot.setQtyIn( quantity +"");
							inventoryLot.setStatus(LOT_STATUS_RUNNING);
							inventoryLot.setVendorName( vendorName );
							inventoryLot.setStage( wipStatus.getMesStep());
							inventoryLot.setType(INVENTORY_TYPE_DIE_BANK);
							inventoryLot.setFabLotNumber(lotNumber);
						}
                    }
                }

                /*Assembly Lots*/
                if( wipStatus.getMfgArea() != null && "ASSY".equalsIgnoreCase( wipStatus.getMfgArea()) && mktPartNumber != null )
                {
                    partNumber = NewAdaptorDAO.getPartNumber( mktPartNumber, WIP_TYPE_ASSEMBLY, WIP_TYPE_ASSEMBLY );

                    if ( partNumber == null)
                    {
                        partNumber = mktPartNumber;
                    }

                    if( partNumber != null )
                    {
                        String vendorName = null;
                        if ( wipStatus.getMfgLoc() != null && "R".equalsIgnoreCase(wipStatus.getMfgLoc()))
                        {
                            vendorName = VENDOR_NAME_CML;
                        }
                        else if ( wipStatus.getMfgLoc() != null && "T".equalsIgnoreCase(wipStatus.getMfgLoc()))
                        {
                            vendorName = VENDOR_NAME_OSET;
                        }
                        else if ( wipStatus.getMfgLoc() != null && "M".equalsIgnoreCase(wipStatus.getMfgLoc()))
                        {
                            vendorName = VENDOR_NAME_ATP;
                        }
                        else if ( wipStatus.getMfgLoc() != null && "L".equalsIgnoreCase(wipStatus.getMfgLoc()))
                        {
                            vendorName = VENDOR_NAME_ATK;
                        }

                        assemblyLot = (AssemblyLot)m_assemblyDataMap.get( lotNumber + partNumber + vendorName );
                        if( assemblyLot == null )
                        {
                            assemblyLot = new AssemblyLot();
                            m_assemblyDataMap.put( lotNumber + partNumber + vendorName, assemblyLot );
                        }

                        int quantity = 0;
                        if ( assemblyLot.getQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt(assemblyLot.getQtyIn());
                            }
                            catch (NumberFormatException npe)
                            {
                            }
                            try
                            {
                                quantity += Integer.parseInt(wipStatus.getMesQty());
                            }
                            catch (NumberFormatException npe)
                            {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt( wipStatus.getMesQty() );
                            }
                            catch (NumberFormatException npe)
                            {
                            }
                        }

                        if( vendorName != null && partNumber != null && lotNumber != null && quantity > 0 )
                        {
                            if( VENDOR_NAME_OSET.equalsIgnoreCase(vendorName))
                            {
                                wipStatus.setDevice(partNumber);
                                osetAsssemblyList.add(wipStatus);
                            }
                            else
                            {
                                assemblyLot.setLotNumber( lotNumber );
                                assemblyLot.setDeviceName( partNumber );
                                assemblyLot.setDateIn( wipStatus.getStartDate() );
                                assemblyLot.setQtyIn( quantity +"");
                                assemblyLot.setStage( wipStatus.getMesStep() );
                                assemblyLot.setVendorName( vendorName );
                                assemblyLot.setStatus( LOT_STATUS_RUNNING);
                                assemblyLot.setFabLotNumber( lotNumber );
                            }
                        }
                    }

                }
                /*Test Lots*/
                if( wipStatus.getMfgArea() != null && "TEST".equalsIgnoreCase( wipStatus.getMfgArea()) && mktPartNumber != null )
                {
                    partNumber = NewAdaptorDAO.getPartNumber( mktPartNumber, LOCATION_TYPE_FT , LOCATION_TYPE_TEST );

                    if ( partNumber == null)
                    {
                        partNumber = mktPartNumber;
                    }

                    String vendorName = null;
                    if ( wipStatus.getMfgLoc() != null && "R".equalsIgnoreCase(wipStatus.getMfgLoc()))
                    {
                        vendorName = VENDOR_NAME_CML;
                    }
                    if ( wipStatus.getMfgLoc() != null && "T".equalsIgnoreCase(wipStatus.getMfgLoc()))
                    {
                        vendorName = VENDOR_NAME_OSET;
                    }
                    if ( wipStatus.getMfgLoc() != null && "M".equalsIgnoreCase(wipStatus.getMfgLoc()))
                    {
                        vendorName = VENDOR_NAME_ATP;
                    }
                    if ( wipStatus.getMfgLoc() != null && "L".equalsIgnoreCase(wipStatus.getMfgLoc()))
                    {
                        vendorName = VENDOR_NAME_ATK;
                    }

                    if( partNumber != null && lotNumber != null && vendorName != null  )
                    {
                        testLot = (TestLot) m_testDataMap.get(lotNumber + partNumber + vendorName );

                        if (testLot == null)
                        {
                            testLot = new TestLot();
                            m_testDataMap.put(lotNumber + partNumber + vendorName, testLot);
                        }

                        int quantity = 0;
                        if (testLot.getQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt(testLot.getQtyIn());
                            }
                            catch (NumberFormatException npe) {
                            }
                            try
                            {
                                quantity += Integer.parseInt(wipStatus.getMesQty());
                            }
                            catch (NumberFormatException npe) {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt(wipStatus.getMesQty());
                            }
                            catch (NumberFormatException npe) {
                            }
                        }
						if ( quantity > 0 )
						{
							testLot.setLotNumber(lotNumber);
							testLot.setPartNumber(partNumber);
							testLot.setDateIn(wipStatus.getStartDate());
							testLot.setQtyIn( quantity+"");
							testLot.setStage(wipStatus.getMesStep());
							testLot.setVendorName(vendorName);
							testLot.setStatus( LOT_STATUS_RUNNING );
							testLot.setPsType( LOCATION_TYPE_FT );
                            testLot.setFabLotNumber( lotNumber );
                        }
                    }
                }
                /*FPO Lots
                *
                * For FPO Process, we setting the dafault vedor as "CML"
                */
                if( wipStatus.getMfgArea() != null && "FPO".equalsIgnoreCase( wipStatus.getMfgArea()) )
                {

                    partNumber = NewAdaptorDAO.getPartNumber( mktPartNumber, LOCATION_TYPE_FPO , LOCATION_TYPE_TEST );

                    if ( partNumber == null)
                    {
                        partNumber = mktPartNumber;
                    }

                    String vendorName = null;
                    if( wipStatus.getMfgLoc() != null )
                    {
                        vendorName = VENDOR_NAME_CML;
                    }

                    if( partNumber != null && lotNumber != null && vendorName != null )
                    {

                        testLot = ( TestLot) m_testDataMap.get( lotNumber + partNumber );

                        if( testLot == null )
                        {
                            testLot = new TestLot();
                            m_testDataMap.put( lotNumber + partNumber, testLot );
                        }

                        int quantity = 0;
                        if( testLot.getQtyIn() != null )
                        {
                            try
                            {
                                quantity = Integer.parseInt(testLot.getQtyIn());
                            }
                            catch (NumberFormatException npe)
                            {
                            }
                            try
                            {
                                quantity += Integer.parseInt( wipStatus.getMesQty() );
                            }
                            catch (NumberFormatException npe)
                            {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt( wipStatus.getMesQty());
                            }
                            catch( NumberFormatException npe )
                            {
                            }
                        }

						 if ( quantity > 0 )
						 {
						 
							testLot.setLotNumber( lotNumber );
							testLot.setPartNumber( partNumber );
							testLot.setDateIn( wipStatus.getStartDate());
							testLot.setQtyIn( quantity +"");
							testLot.setStatus(LOT_STATUS_RUNNING);
							testLot.setVendorName( vendorName );
							testLot.setStage( wipStatus.getMesStep());
							testLot.setPsType(LOCATION_TYPE_FPO );
                            testLot.setFabLotNumber( lotNumber );
                         }
                    }

                }
                /*TR Lots*/
                if( wipStatus.getMfgArea() != null && "TR".equalsIgnoreCase( wipStatus.getMfgArea()) )
                {
                    partNumber = NewAdaptorDAO.getPartNumber( mktPartNumber, LOCATION_TYPE_TAPE_REEL, LOCATION_TYPE_CLASS_STORE);

                    if ( partNumber == null)
                    {
                        partNumber = mktPartNumber;
                    }

                    String vendorName = null;
                    if( wipStatus.getMfgLoc() != null && "R".equalsIgnoreCase( wipStatus.getMfgLoc()))
                    {
                        vendorName = VENDOR_NAME_CML;
                    }

                    if( partNumber != null && lotNumber != null && vendorName != null )
                    {

                        inventoryLot = ( InventoryLot) m_trDataMap.get( lotNumber + partNumber );
                        if( inventoryLot == null )
                        {
                            inventoryLot = new InventoryLot();
                            m_trDataMap.put( lotNumber + partNumber, inventoryLot );
                        }

                        int quantity = 0;
                        if( inventoryLot.getQtyIn() != null )
                        {
                            try
                            {
                                quantity = Integer.parseInt(inventoryLot.getQtyIn());
                            }
                            catch (NumberFormatException npe)
                            {
                            }
                            try
                            {
                                quantity += Integer.parseInt( wipStatus.getMesQty() );
                            }
                            catch (NumberFormatException npe)
                            {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt( wipStatus.getMesQty());
                            }
                            catch( NumberFormatException npe )
                            {
                            }
                        }

						if ( quantity > 0 )
						{
							inventoryLot.setLotNumber( lotNumber );
							inventoryLot.setPartNumber( partNumber );
							inventoryLot.setDateIn( wipStatus.getStartDate());
							inventoryLot.setQtyIn( quantity +"");
							inventoryLot.setStatus(LOT_STATUS_RUNNING);
							inventoryLot.setVendorName( vendorName );
							inventoryLot.setStage( wipStatus.getMesStep());
							inventoryLot.setType( LOCATION_TYPE_CLASS_STORE);
							inventoryLot.setFabLotNumber(lotNumber);
						}
                    }

                }
                /*FGC Lots*/
                if( wipStatus.getMfgArea() != null && ( "FGC".equalsIgnoreCase( wipStatus.getMfgArea()) || "FGRW".equalsIgnoreCase( wipStatus.getMfgArea()) ) && mktPartNumber != null)
                {
                     String vendorName = null;
                    if( wipStatus.getMfgLoc() != null && "R".equalsIgnoreCase( wipStatus.getMfgLoc()) || "GR".equalsIgnoreCase( wipStatus.getMfgLoc()))
                    {
                        vendorName = VENDOR_NAME_CML;
                    }

                    if ( mktPartNumber != null && lotNumber != null && vendorName != null )
                    {
                        inventoryLot = ( InventoryLot) m_fgDataMap.get( lotNumber + mktPartNumber );

                        if( inventoryLot == null )
                        {
                            inventoryLot = new InventoryLot();
                            m_fgDataMap.put( lotNumber + mktPartNumber, inventoryLot );
                        }

                        int quantity = 0;
                        if( inventoryLot.getQtyIn() != null )
                        {
                            try
                            {
                                quantity = Integer.parseInt(inventoryLot.getQtyIn());
                            }
                            catch (NumberFormatException npe)
                            {
                            }
                            try
                            {
                                quantity += Integer.parseInt( wipStatus.getMesQty() );
                            }
                            catch (NumberFormatException npe)
                            {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt( wipStatus.getMesQty());
                            }
                            catch( NumberFormatException npe )
                            {
                            }
                        }

						if ( quantity > 0 )
						{
							inventoryLot.setLotNumber( lotNumber );
							inventoryLot.setPartNumber( mktPartNumber );
							inventoryLot.setDateIn( wipStatus.getStartDate());
							inventoryLot.setQtyIn( quantity +"");
							inventoryLot.setStatus(LOT_STATUS_RUNNING);
							inventoryLot.setVendorName( vendorName );
							inventoryLot.setStage( wipStatus.getMesStep());
							inventoryLot.setType( LOCATION_TYPE_FINISHED_GOODS );
							inventoryLot.setExpectedDate(wipStatus.getOutDate());
							inventoryLot.setFabLotNumber(lotNumber);
						}
                    }
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            log("ERROR!ERROR! While parsing the Wip Fed Details" + e);
            System.out.println("ERROR!ERROR! While parsing the Wip Fed Details" + e);
            throw e;
        }
    }

    private void log(String message)
    {
        if (message == null) return;

        if (m_parserLogger != null)
        {
            synchronized (m_parserLogger)
            {
                m_parserLogger.logDebug(message + LINE_SEPARATOR);
            }
        }
        else
        {
            System.out.println(message);
        }
    }

    private void printVersionInfo()
    {
        String info = "\n\n+----------- Running Spectra Linear Wip Data Parser on " + new java.util.Date(System.currentTimeMillis()) + " -------------+\n" +
                "\n+---------------------------------------------------------------+\n" +
                "+                                                                \n" +
                "+           Spectra Linear Wip Data Interpreter - Beta          \n" +
                "+                                                                \n" +
                "+    Copy Right (C) 2007 - ScalarSoft Inc., All rights reserved.  \n" +
                "+                                                                \n" +
                "+---------------------------------------------------------------+\n";
        System.out.println(info);
        log(info);
    } //end of printVersionInfo
}
